package com.lxq.LeetCode.isValid;

import java.util.Stack;

public class Solution {
    public static void main(String[] args) {
        System.out.println(isValid("{()}"));
    }

    public static boolean isValid(String s) {
        Stack<Character> symbolStack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
                symbolStack.push(s.charAt(i));
            } else if (!symbolStack.empty()) {
                char c = symbolStack.pop();
                if (Math.abs(s.charAt(i) - c) > 2) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return symbolStack.empty();
    }
}
